﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Popups;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using System.Runtime.Serialization;


// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace TestApp
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        
        movements move = new movements();
        networkAccess nA = new networkAccess();
        imageChanger imgCh = new imageChanger();

       // Windows.Networking.HostName hN = new Windows.Networking.HostName("192.168.1.252");

        double leftWheelDat;
        double rightWheelDat;
        double camerAngelDat;
        string left;
        string right;
        string cAngle;
        bool isEqual;
        string kinBot = "192.168.2.200";
        string androidPhone = "192.168.2.201";
        string server = "192.168.2.252";
        private bool ipSet = false;
        private bool lights = false;
        private bool rmtLoaded = false;

        Uri androidCam;
        Uri androidRMT;

        public MainPage()
        {
            
            this.InitializeComponent();
            
            //MessageDialog startDialog = new MessageDialog("Please input the IP address of the FEZ and Android IP WebCam.");
            shMsg("Please input the IP address of the FEZ and Android IP WebCam.");
           // startDialog.ShowAsync();
                //nA.NetworkInterface();
            //Windows.Networking.HostName hN = new Windows.Networking.HostName(kinBot);
            //nA.connect(hN, "2000");

        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void connectRobot()
        {
            if (ipSet == true)
            {
                if (kinBot != null && androidPhone != null && server != null)
                {

                    Windows.Networking.HostName hN = new Windows.Networking.HostName(kinBot);
                    nA.connect(hN, "2000");
                }

                else
                {
                    shMsg("Please provide an IP address for the Robot and Android Phone.");
                }
            }
            else
            {
                shMsg("Please provide an IP address for the Robot and Android Phone.");
            }
        }

        async private void shMsg(string s)
        {
            MessageDialog startDialog = new MessageDialog(s);
            await startDialog.ShowAsync();
        }

        private void leftWheel_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
        {
            if (ipSet == true)
            {
                if (isEqual != true)
                {


                    leftWheelDat = e.NewValue;
                    left = "Y" + leftWheelDat.ToString();
                }

                else
                {
                    leftWheelDat = e.NewValue;
                    left = "Y" + leftWheelDat.ToString();
                    if (leftWheelDat > 10)
                    {
                        double r = leftWheelDat - 10;
                        right = "X" + r.ToString();
                    }
                    else
                    {
                        right = "X" + leftWheelDat.ToString();
                    }
                    nA.sendData(right);
                    nA.sendData(left);

                }

                //left = leftWheelDat.ToString();

                //leftData.Text = left;
                nA.sendData(left);
            }
        }

        private void rightWheel_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
        {
            if (ipSet == true)
            {

                if (isEqual != true)
                {
                    rightWheelDat = e.NewValue;
                    right = "X" + rightWheelDat.ToString();
                }
                else
                {
                    right = "X" + leftWheelDat.ToString();
                }
                // right = rightWheelDat.ToString();
                //move.rightWheel = right;
                //rightData.Text = right;
                nA.sendData(right);
            }
         
        }

        private void motorZero_Click(object sender, RoutedEventArgs e)
        {
            rightWheel.Value = 0;
            nA.sendData("X0");
            leftWheel.Value = 0;
            nA.sendData("Y0");
        }

        private void motorEqual_Click(object sender, RoutedEventArgs e)
        {
            if (isEqual == true)
            {
                recEqual.Text = "false";
                isEqual = false;
            }
            else if (isEqual == false)
            {
                recEqual.Text = "true";
                isEqual = true;
            }
        }

        private void cameraAngel_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
        {
            camerAngelDat = e.NewValue;
            cAngle = "Z" + camerAngelDat.ToString();
        
            nA.sendData(cAngle);

        }

        private void submit_Click(object sender, RoutedEventArgs e)
        {
            var app = App.Current;

            kinBot = kinbotIPInput.Text;
            androidPhone = androidIPInput.Text;

            ipSet = true;

            App.allSet = true;

            connectRobot();

            androidCam = new Uri("http://" + androidPhone + ":8080/jsfs.html");
            androidRMT = new Uri("http://" + androidPhone + ":8080/remote.html");
            ipCam.Navigate(androidCam);
            rmt.Navigate(androidRMT);
        }

        private void lgtSw_Toggled(object sender, RoutedEventArgs e)
        {   
            if (lights == false)
            {
                lights = true;
                lightBulb.Source = imgCh.setImages("lightOn");
                if (rmtLoaded == true)
                {
                    try
                    {
                        rmt.InvokeScript("ipwajax", new string[] { "enabletorch" });
                    }
                    catch (Exception ex)
                    {
                        //  alert1.Text = "Could not call script: " +
                        // ex.Message +
                        //"\n\nPlease click the 'Load HTML Document with Script' button to load.";

                    }
                }
            }
                else 
                {
                    lights = false;
                    lightBulb.Source = imgCh.setImages("lightOff");
                    if (rmtLoaded == true)
                    {
                        try
                        {
                            rmt.InvokeScript("ipwajax", new string[] { "disabletorch" });
                        }
                        catch (Exception ex)
                        {
                            //  alert1.Text = "Could not call script: " +
                            // ex.Message +
                            //"\n\nPlease click the 'Load HTML Document with Script' button to load.";

                        }
                    }
                }
            }
            

           
        

        private void rmt_LoadCompleted(object sender, NavigationEventArgs e)
        {
            rmtLoaded = true;

            camera.Source = imgCh.setImages("cameraOn");

        }





    }
}
